package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.File;
import objects.IObject;

public class FileSection implements ISection
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8634025139316291413L;
	
	private Map<String, File> fileList;
	
	public FileSection()
	{
		fileList = new LinkedHashMap<String, File>();
	}

	@Override
	public int getCount()
	{
		return fileList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.FILE;
	}

	@Override
	public IObject[] getObjects()
	{
		return fileList.values().toArray(new IObject[]{});
	}
	
	public File getFile(String name)
	{
		return fileList.get(name);
	}
	
	public File createNewFile(String name)
	{
		File file = new File(name);
		
		fileList.put(name, file);
		
		return file;
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void validateState()
	{
		// TODO Auto-generated method stub
		
	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}

	public void removeFile(File iObject)
	{
		fileList.remove(iObject.getObjectName());
	}
	
	@Override
	public String toString()
	{
		StringBuffer fileSecDef = new StringBuffer(fileList.size() * 200);

		fileSecDef.append(sNewLine);

		for (String fileName : fileList.keySet())
		{
			File refType = fileList.get(fileName);

			fileSecDef.append(refType.toString());
			fileSecDef.append(sNewLine);
		}

		return fileSecDef.toString();
	}

}
